﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InformationSystem.BLL.Core.Model;
using InformationSystem.BLL.Core.Service;

namespace InformationSystem.WebSite
{
    public partial class Panel : BasePage
    {
        protected UserType CurrentUserType;
        protected LoginUser user;
        public IInformationService InformationService { get; set; }
        public IUserService UserService { get; set; }
        public ICollegeService CollegeService { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            user = (LoginUser)Session["LoginUser"];
            lastlogintime.InnerText = string.Format("最后登录时间:{0}", user.LastLoginDateTime.ToString("yyyy/MM/dd HH:mm"));
            CurrentUserType = user.LoginUserType;

            if (Request.QueryString["m"] != null)
            {
                string m = Request.QueryString["m"];
                if (m != string.Empty)
                {
                    switch (m.ToLower())
                    {
                        case "mail":
                            hdnuri.Value = "Module/MailMgr/MailIn.aspx";
                            hdnm.Value = m.ToLower();
                            break;
                        case "student":
                            if (user.LoginUserType == UserType.Assistant)
                            {
                                hdnuri.Value = "Module/StuMgr/StuMgr.aspx";
                                hdnm.Value = m.ToLower();
                            }
                            break;
                        case "document":
                            hdnuri.Value = "Module/DocumentMgr/DocumentEdit.aspx";
                            hdnm.Value = m.ToLower();
                            break;
                        case "information":
                            hdnuri.Value = "Module/NoticeMgr/MessageMgr.aspx";
                            hdnm.Value = m.ToLower();
                            break;
                        case "workinfo":
                            hdnuri.Value = "Module/NoticeMgr/WorkInfMgr.aspx";
                            hdnm.Value = "information";
                            break;
                        case "affiche":
                            hdnuri.Value = "Module/NoticeMgr/AfficheMgr.aspx";
                            hdnm.Value = "information";
                            break;
                        case "reg":
                            hdnuri.Value = "Module/SysMgr/SysMgrMain.aspx";
                            hdnm.Value = "systemreg";
                            break;
                    }

                }
            }
            this.GetSysInfo();
            taskmgr.Tasks[0].Interval = (int)Application[Common.ClientRefreshSpan];
        }

        private void GetSysInfo()
        {
            if (user.LoginUserType == UserType.Admin)
            {
                DateTime start = (DateTime)Application[Common.StartTime];
                TimeSpan sp = DateTime.Now - start;
                lastsys.InnerText = string.Format("{0}天{1}时{2}分{3}秒", sp.Days, sp.Hours, sp.Minutes, sp.Seconds);
                lastvisited.InnerText = Application[Common.VisitedCount].ToString();
                totalstucount.InnerText = UserService.GetAllStudentCount().ToString();
                totalasscount.InnerText = UserService.GetAllAssistantCount().ToString();
                todayvisited.InnerText = ((Visited)Application[Common.VisitedToday]).Count.ToString();
                onlineusercount.InnerText = base.OnLineUserCount.ToString();
            }
            else if (user.LoginUserType == UserType.Assistant)
            {

                pubinfocount.InnerText = InformationService.GetAfficheCountInGrade(base.LoginUser).ToString();
                gradestucount.InnerText = UserService.GetStudentCountInGrade(base.LoginUser).ToString();
                asscount.InnerText = UserService.GetCollegeAssistant(base.LoginUser).Count.ToString();
                deponlinecount.InnerText = OnLineGradeUserCount.ToString();
                College college = CollegeService.GetCollegeInfo(base.LoginUser.CollegeID);
                if (college == null)
                {
                    forum.InnerText = "未知";
                }
                else
                {
                    forum.InnerText = college.UserForum ? "是" : "否";
                }
            }
            else
            {
                string tmp = "";
                IList<string> asslist = UserService.GetCollegeAssistant(base.LoginUser);
                foreach (var item in asslist)
                {
                    tmp += item + " ";
                }
                assname.InnerText = tmp.TrimEnd(' ');
                gradestucountforparent.InnerText = UserService.GetStudentCountInGrade(base.LoginUser).ToString();

                gradeonlinestu.InnerText = base.OnLineGradeStudentCount.ToString();
                gradeonlineparent.InnerText = base.OnLineGradeParentCount.ToString();
                gradeass.InnerText = base.OnLineGradeAssistantUser;

                int year = DateTime.Now.Year;
                int month = DateTime.Now.Month;

                int distance = year - base.LoginUser.GradeID;
                string up = (month > 8 && month < 3) ? "上" : "下";
                switch (distance)
                {
                    case 0:
                        if (up == "上")
                        {
                            currentterm.InnerText = "大一上";
                        }
                        break;
                    case 1:
                        if (up == "下")
                        {
                            currentterm.InnerText = "大一下";
                        }
                        else
                        {
                            currentterm.InnerText = "大二上";
                        }
                        break;
                    case 2:
                        if (up == "下")
                        {
                            currentterm.InnerText = "大二下";
                        }
                        else
                        {
                            currentterm.InnerText = "大三上";
                        }
                        break;
                    case 3:
                        if (up == "下")
                        {
                            currentterm.InnerText = "大三下";
                        }
                        else
                        {
                            currentterm.InnerText = "大四上";
                        }
                        break;
                    case 4:
                        if (up == "下")
                        {
                            currentterm.InnerText = "大四下";
                        }
                        else
                        {
                            currentterm.InnerText = "已毕业";
                        }
                        break;
                    default:
                        currentterm.InnerText = "已毕业";
                        break;

                }


            }
        }

        protected override bool CommitPermissionCheck()
        {
            throw new NotImplementedException();
        }
    }
}